<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索和添加区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query"
          clearable @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
        </el-row>
        <!-- 表格区 -->
        <el-table :data="goodslist" style="width: 100%" stripe border>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="500"></el-table-column>
          <el-table-column prop="goods_price" label="商品(价格)"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
          <el-table-column label="创建时间">
            <template v-slot="scope">
              {{scope.row.add_time|dataFormate}}
              </template>
            </el-table-column>           
            <el-table-column label="操作">
                    <template v-slot="scope">
<!-- {{scope.row.goods_id}} -->
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" 
              @click="removeGoodsById(scope.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 获取商品分类的查询参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10,
      },
      //   商品列表的数组
      goodslist: [],
      // 总数
      total: 0,
    };
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表信息失败");
      }
      this.goodslist = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newVal) {
      this.queryInfo.pagesize = newVal;
      // 重新发送请求
      this.getGoodsList()
    },
    // pagenum发生变化触发的函数
    handleCurrentChange(newVal) {
      this.queryInfo.pagenum = newVal;
      // 重新发生请求
      this.getGoodsList();
    },
    async removeGoodsById(id) {
      const result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // confirm表示点击的是确认按钮
      // cancel表示点击是取消按钮
      if (result != "confirm") {
        return this.$message.info("已删除");
      }
      // 发送删除请求
      const { data: res } = await this.$http.delete(`goods/${id}`);
      if (res.meta.status !== 200) {
        return this.$message.error("删除失败，稍后尝试");
      }
      this.$message.success("删除成功");
      // 重新加载
      this.getGoodsList();
    },
  },
  created() {
    // 调用商品列表的方法
    this.getGoodsList();
  },
};
</script>

<style lang="less" scoped>
</style>